package com.rr.dao;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Repository;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author RR
 * @date 2023/3/15 23:56
 */
public class UserDao {

    private Map<String, User> userListMap;

    /*
     * mock几个用户名，模拟从数据库查出数据
     * */
    public UserDao(PasswordEncoder passwordEncoder){
        userListMap = new HashMap<>();
        List<GrantedAuthority> adminAuths = AuthorityUtils.commaSeparatedStringToAuthorityList("admin");
        List<GrantedAuthority> normalAuths = AuthorityUtils.commaSeparatedStringToAuthorityList("normal");
        List<GrantedAuthority> roleAuths = AuthorityUtils.commaSeparatedStringToAuthorityList("ROLE_role");
        User user1 = new User("user1", passwordEncoder.encode("123"), adminAuths);
        User user2 = new User("user2", passwordEncoder.encode("123"), adminAuths);
        User user3 = new User("user3", passwordEncoder.encode("123"), normalAuths);
        User user4 = new User("user4", passwordEncoder.encode("123"), roleAuths);
        userListMap.put(user1.getUsername(), user1);
        userListMap.put(user2.getUsername(), user2);
        userListMap.put(user3.getUsername(), user3);
        userListMap.put(user4.getUsername(), user4);
    }

    public User getUser(String username) {
        return userListMap.get(username);
    }
}
